package com.andy.common.db

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.andy.base.AppHelper
import com.andy.common.db.entity.UserEntity
import com.andy.common.db.dao.UserDao

/**
 * Author:Andy
 * Date:2023/3/21 15:56
 * Description:AppDataBase
 */
@Database(entities = [UserEntity::class], version = 1, exportSchema = false)
abstract class AppDB : RoomDatabase() {
    abstract fun userDao(): UserDao

    companion object {
        private const val DB_NAME = "avatar_room.db"

        @Volatile
        private var instance: AppDB? = null
        fun getInstance(): AppDB {
            return instance ?: synchronized(this) {
                instance ?: buildDataBase().also {
                    instance = it
                }
            }
        }

        private fun buildDataBase(): AppDB {
            return Room.databaseBuilder(AppHelper.mContext, AppDB::class.java, DB_NAME)
                .allowMainThreadQueries()
                .build()
        }
    }
}